package com.haohope.framework.datasource;

import java.util.Properties;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.haohope.framework.FrameworkConstant;

public class PrimaryDataSource extends DruidDataSource{
	/**
	 * 
	 */
	private static final long serialVersionUID = -7745984646341558L;
	private final static Log LOG = LogFactory.getLog(PrimaryDataSource.class);
	public PrimaryDataSource() {
		super(false);
	}
	public PrimaryDataSource(boolean fairLock){
        super(fairLock);
    }
	public void configFromPropety(Properties properties) {
		LOG.info("config default db.");
		String driverClassName = FrameworkConstant.SystemParam.getDbDriverClassName();
		String dbUrl = FrameworkConstant.SystemParam.getDbUrl();
		String dbUsername = FrameworkConstant.SystemParam.getDbUsername();
		String dbPassword = FrameworkConstant.SystemParam.getDbPasswd();
		this.setName("default");
		this.setDriverClassName(driverClassName);
		this.setUrl(dbUrl);
		this.setUsername(dbUsername);
		this.setPassword(dbPassword);
		/**
		{
            String property = properties.getProperty("druid.driverClassName");
            if (property != null) {
                this.setDriverClassName(property);
            }else if(debug){
            	this.setDriverClassName("com.mysql.jdbc.Driver");
            }
        }
        {
            String property = properties.getProperty("druid.url");
            if (property != null) {
                this.setUrl(property);
            }else if(debug){
            	this.setUrl("jdbc:mysql://127.0.0.1:3306/db_wb?useSSL=false&characterEncoding=utf8");
            }
        }
        {
            String property = properties.getProperty("druid.username");
            if (property != null) {
                this.setUsername(property);
            }else if(debug){
            	this.setUsername("root");
            }
        }
        {
            String property = properties.getProperty("druid.password");
            if (property != null) {
                this.setPassword(property);
            }else if(debug){
            	this.setPassword("123456");
            }
        }
        ***/
        /***
        {
            Boolean value = getBoolean(properties, "druid.testWhileIdle");
            if (value != null) {
                this.testWhileIdle = value;
            }else {
            	this.testWhileIdle = true;
            }
        }
        {
        	//程序 申请 连接时,进行连接有效性检查（低效，影响性能）
            Boolean value = getBoolean(properties, "druid.testOnBorrow");
            if (value != null) {
                this.testOnBorrow = value;
            }else {
            	this.testOnBorrow = false;
            }
        }
        {
        	//程序 返还 连接时,进行连接有效性检查（低效，影响性能）
            Boolean value = getBoolean(properties, "druid.testOnReturn");
            if (value != null) {
                this.testOnReturn = value;
            }else {
            	this.testOnReturn = false;
            }
        }
        {
            String property = properties.getProperty("druid.validationQuery");
            if (property != null && property.length() > 0) {
                this.setValidationQuery(property);
            }else {
            	this.setValidationQuery("SELECT 'x' ");
            }
        }
        {
        	//配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
        	Long timeBetweenEvictionRunsMillis = Utils.getLong(properties,"druid.timeBetweenEvictionRunsMillis");
            if(null==timeBetweenEvictionRunsMillis) {
            	timeBetweenEvictionRunsMillis = 60000l;
            }
            this.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        }
        {
            //配置获取连接等待超时的时间
        	Integer maxWait = Utils.getInteger(properties,"druid.maxWait");
        	//60000
        	if(null==maxWait) {
        		maxWait = 60000;
        	}
        	this.setMaxWait(maxWait);
        }
        {
            //池中某个连接的空闲时长达到 N 毫秒后, 连接池在下次检查空闲连接时，将回收该连接,要小于防火墙超时设置
        	Long minEvictableIdleTimeMillis = Utils.getLong(properties, "druid.minEvictableIdleTimeMillis");
        	if(null==minEvictableIdleTimeMillis) {
        		minEvictableIdleTimeMillis = 300000l;
        	}
        	this.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        }
        {
            //初始化连接大小
        	Integer initialSize = Utils.getInteger(properties,"druid.initialSize");
        	if(null==initialSize) {
        		initialSize = 10;
        	}
        	this.setInitialSize(initialSize);//
        }
        {
        	//连接池最小空闲
        	Integer minIdle = Utils.getInteger(properties,"druid.minIdle");
        	if(null==minIdle) {
        		minIdle = 10;
        	}
        	this.setMinIdle(minIdle);
        }
        {
        	//连接池最大使用连接数
        	Integer maxActive = Utils.getInteger(properties,"druid.maxActive");
        	if(null==maxActive) {
        		maxActive = 20;
        	}
        	this.setMaxActive(maxActive);
        }
        {
        	//每个连接最多缓存多少个SQL 20
        	Integer maxPoolPreparedStatementPerConnectionSize = Utils.getInteger(properties,"druid.maxPoolPreparedStatementPerConnectionSize");
        	if(null==maxPoolPreparedStatementPerConnectionSize) {
        		maxPoolPreparedStatementPerConnectionSize = 20;
        	}
        	this.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        }
        {
        	//这里配置的是插件,常用的插件有: 	stat,wall,slf4j
          	//监控统计: filter:stat 	 
          	//日志监控: filter:log4j 或者 slf4j 	 
          	//防御SQL注入: filter:wall
            String property = properties.getProperty("druid.filters");
            if (property != null && property.length() > 0) {
                try {
                    this.setFilters(property);
                } catch (SQLException e) {
                    LOG.error("setFilters error", e);
                }
            }else {
            	try {
                    this.setFilters("stat,wall,log4j,config");
                } catch (SQLException e) {
                    LOG.error("setFilters error", e);
                }
            }
        }
        {
        	//连接属性。比如设置一些连接池统计方面的配置。 	 
          	//druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
            //String property = properties.getProperty("druid.connectProperties");
            //if (property != null) {
            //    this.setConnectionProperties(property);
            //}else {
            //	this.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
            //}
        }
        ***/
        DataSourceBuilder.configFromPropety(this, properties);
        super.configFromPropety(properties);
    }
	/*
	#           initialSize 初始化连接大小
	#		    minIdle 连接池最小空闲
	#		    maxActive 连接池最大使用连接数
	#	        maxWait 配置获取连接等待超时的时间
	#		    timeBetweenEvictionRunsMillis 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
	#		    minEvictableIdleTimeMillis 配置一个连接在池中最小生存的时间，单位是毫秒
	#		    removeAbandoned 自动清除无用连接
	#		    removeAbandonedTimeout 清除无用连接的等待时间 
	#		    validationQuery
	#		    testWhileIdle
	#		    testOnBorrow 申请连接时执行validationQuery检测连接是否有效
	#		    testOnReturn 归还连接时执行validationQuery检测连接是否有效
	#		    poolPreparedStatements 打开PSCache，并且指定每个连接上PSCache的大小
	#		    maxPoolPreparedStatementPerConnectionSize
	*/
}
